import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testclusterrolebinding:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/1_get_clusterrolebinding_list.yaml'))
    def test_get_clusterrolebinding_list(self, clusterrolebindinginfo):
        # 获取clusterrolebinding列表接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/1_get_clusterrolebinding_list_exception.yaml'))
    def test_get_clusterrolebinding_list_exception(self, clusterrolebindinginfo):
        # 获取clusterrolebinding列表接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/2_create_clusterrolebinding.yaml'))
    def test_create_clusterrolebinding(self, clusterrolebindinginfo):
        # 创建clusterrolebinding接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        clusterrolebindingname = req.json()['metadata']['name']
        globals()['clusterrolebindingname'] = clusterrolebindingname
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/2_create_clusterrolebinding_exception.yaml'))
    def test_create_clusterrolebinding_exception_exception(self, clusterrolebindinginfo):
        # 创建clusterrolebinding接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/3_get_specific_clusterrolebinding.yaml'))
    def test_get_specific_clusterrolebinding(self, clusterrolebindinginfo):
        # 获取指定clusterrolebinding接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/3_get_specific_clusterrolebinding_exception.yaml'))
    def test_get_specific_clusterrolebinding_exception(self, clusterrolebindinginfo):
        # 获取指定clusterrolebinding接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/4_update_clusterrolebinding.yaml'))
    def test_update_clusterrolebinding(self, clusterrolebindinginfo):
        # 更新指定clusterrolebinding接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        clusterrolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/4_update_clusterrolebinding_exception.yaml'))
    def test_update_clusterrolebinding_exception(self, clusterrolebindinginfo):
        # 更新指定clusterrolebinding接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        clusterrolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = clusterrolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/5_update_section_clusterrolebinding.yaml'))
    def test_update_section_clusterrolebinding(self, clusterrolebindinginfo):
        # 部分更新指定clusterrolebinding的接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        header = clusterrolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/5_update_section_clusterrolebinding_exception.yaml'))
    def test_update_section_clusterrolebinding_exception(self, clusterrolebindinginfo):
        # 部分更新指定clusterrolebinding的接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        data = clusterrolebindinginfo['request']['data']
        header = clusterrolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/6_delete_specific_clusterrolebinding.yaml'))
    def test_delete_specific_clusterrolebinding(self, clusterrolebindinginfo):
        # 删除clusterrolebinding接口-正常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.clusterrolebinding
    @pytest.mark.parametrize('clusterrolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/clusterrolebinding/6_delete_specific_clusterrolebinding_exception.yaml'))
    def test_delete_specific_clusterrolebinding_exception(self, clusterrolebindinginfo):
        # 删除clusterrolebinding接口-异常
        base_url = datas.baseUrl + Testclusterrolebinding.url_unify
        url = base_url + clusterrolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{clusterrolebindingname}', globals()['clusterrolebindingname'])
        method = clusterrolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterrolebindinginfo['validate'][0]['equals']['status_code']
